#include<bits/stdc++.h>
#define int unsigned long long
using namespace std;

int gcd(int a,int b){
	return b?gcd(b,a%b):a;
}

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0); 
	int d,p;
	cin>>d>>p;
	int g=gcd(d,p);
	int m=p/g;
	int t;
	cin>>t;
	while(t--) {
		int l,r;
		cin>>l>>r;
		int len=r-l+1;
		if(p==1) {
			cout<<2<<endl;
			continue;
		if(m==1)cout<<1<<endl; 
		} else {
			if(len>=m) {
				cout<<m<<endl;
			} else {
				int s=l%m;
				int e=r%m;
				int cnt;
				if(s<=e) {
					cnt=e-s+1;
				} else {
					cnt=m-s+e+1;
				}
				cout<<cnt<<endl;
			}
		}


	}
	return 0;
}
